package fr.upmc.cie;

import java.util.ArrayList;

import fr.upmc.cie.connexion.Updater;
import fr.upmc.cie.data.CategorieManager;
import fr.upmc.cie.data.ChannelManager;
import fr.upmc.cie.data.DataManager;
import fr.upmc.cie.data.TicketItem;
import fr.upmc.cie.ext.MainPageExpandableListAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;

public class Portail extends Activity {
	
	private CategorieManager categorieManager;
	private MainPageExpandableListAdapter cea;
	private ChannelManager channelManager;
	private ArrayList<TicketItem> recent ;
	private ExpandableListView elv;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        setContentView(R.layout.mianpage);

       elv = (ExpandableListView) this.findViewById(R.id.mainpage_elv);
        
        categorieManager = DataManager.getCategorieManager(this);
        channelManager = DataManager.getChannelManager(this);
        
        
        //Mettre a jour les tickets
        Updater.refresh(this);
        
        //Obtenir les 5 derniers tickets
        recent = new ArrayList<TicketItem>();
        Updater.getRecentUpdate(5, recent, this);
        
        cea = new MainPageExpandableListAdapter(this,
        										recent,
        										categorieManager.getAllCategories(),
        										channelManager.getAllChannels());
        elv.setAdapter(cea);
        elv.expandGroup(0);
        
        elv.setOnChildClickListener(new OnChildClickListener(){
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				/*
				 * cliquer sur les tickets recent
				 */
				if (groupPosition==0){
					Intent i = new Intent();
					Bundle extras = new Bundle();
					TicketItem ticket = recent.get(childPosition);
					extras.putSerializable("ticket", ticket);
					i.putExtras(extras);
					i.setClass(v.getContext(), TicketDisplay.class);
					startActivity(i);
					return true;
				}
				/*
				 * cliquer sur les categories
				 */
				if (groupPosition==1){
					Intent i = new Intent();
					i.putExtra("categorie", childPosition+1);
					i.putExtra("type", 1);
					i.setClass(v.getContext(), ListTickets.class);
					startActivity(i);
					return true;
				}
				/*
				 * cliquer sur les channels 
				 */
				if (groupPosition==2){
					Intent i = new Intent();
					i.putExtra("channel", childPosition+1);
					i.putExtra("type", 2);
					i.setClass(v.getContext(), ListTickets.class);
					startActivity(i);
					return true;
				}
				return false;
			}
			
        	
        });
        
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.mainpage_menu, menu);
	    return true;
	}
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()){
    		case R.id.mainpagemenu_subscribe:
    			Intent i = new Intent();
    			i.setClass(this, Subscribe.class);
    			startActivityForResult(i,2);
    			return true;
    		case R.id.mainpagemenu_search:
    			return true;
    		case R.id.mainpagemenu_refresh:
   			 	Updater.refresh(this);
   			 	Updater.getRecentUpdate(5, recent, this);
   			 	cea = new MainPageExpandableListAdapter(this,
   			 				recent,
   			 				categorieManager.getAllCategories(),
   			 				channelManager.getAllChannels());
   			 	elv.setAdapter(cea);
   			 	elv.expandGroup(0);
    			return true;
    		case R.id.mainpagemenu_settings:
    			Intent j = new Intent();
    			j.setClass(this, Settings.class);
    			startActivityForResult(j,2);
    			return true;
    		default:
    			return true;

    	}
    }
    
    public void onActivityResult(int requestValue, int resultCode, Intent data){
    	//Mettre a jour les tickets
        Updater.refresh(this);
        
        //Obtenir les 5 derniers tickets
        Updater.getRecentUpdate(5, recent, this);
        
        cea = new MainPageExpandableListAdapter(this,
        										recent,
        										categorieManager.getAllCategories(),
        										channelManager.getAllChannels());
        elv.setAdapter(cea);
        elv.expandGroup(0);
	}
}